<?php

namespace Home\Controller;

class IndexController extends BaseMallController {

    function _empty($name) {
        echo '当前方法' . $name . '不存在';
    }

    /**
     * @api {get} /index.php?m=Home&c=Index&a=index 商城首页接口
     */
    function index() {
        //素材
        $data = array(
            'ads' => array(),
            'rec_store' => array(),
            'rec_goods' => array(),
        );
        $ads = M('app_ad')->order('sort_order desc')->select();
        foreach ($ads as $ad) {
            if (!$data['ads'][$ad['ad_type']]) {
                $data['ads'][$ad['ad_type']] = array();
            }
            $data['ads'][$ad['ad_type']][] = $ad;
        }

        //推荐店铺
        $rec_store = M('store')->where('recommended=1 AND state=1')->order('sort_order asc')->select();
        if ($rec_store) {
            $data['rec_store'] = $rec_store;
        }
        //推荐商品
         $rec_goods = M('goods')->alias('g')->join('__STORE__ s ON s.store_id=g.store_id')->field('g.*')->where('g.mall_recommended=1 AND g.if_show=1 AND g.closed=0 AND s.state=1')->order('mall_sort_order asc')->select();
        if ($rec_goods) {
            $data['rec_goods'] = $rec_goods;
        }
        render('10000', '', $data);
    }

    /**
     * @api {post} /index.php?m=Home&c=Index&a=login 登录相关接口
     * @apiName 登录相关接口
     * @apiGroup All
     * @apiVersion 0.1.0 
     * @apiDescription 登录相关接口
     * 
     * @apiParam {String} user_name 用户名
     * @apiParam {String} password 密码
     * @apiParam {String} password 设备信息
     * 
     * @apiSuccess {String} user_id 日志ID
     * 
     */
    function login() {
        $user_info = array();
        $user_name = I('request.user_name');
        $password = I('request.password');
        $openid = I('request.openid'); //设备信息

        if ($user_name && $password) {
			
			$the_user=M('Member')->where('phone_mob="'.$user_name.'" OR user_name="'.$user_name.'" OR email="'.$user_name.'"')->field('user_name,user_id,s_name,s_id')->find();
			if($the_user){
				$user_name=$the_user['user_name'];
			}else{
				render('10106', '用户不存在');
			}
			
            //验证账号密码
            $user_id = D("Member")->do_auth($user_name, $password);
            
            //登录赠送积分
            D('Integrallog')->change_integral_login($user_id);

            //执行登录操作后，更换一次token  此处先使用明文进行传输，后期使用加密算法进行验证。
            $token = session_id();
            //保存到app_token表
            M('AppToken')->add(array(
                'user_id' => $user_id,
                'token' => $token
            ));

            $store = M('store')->where(array('store_id'=>$the_user['s_id']))->find();

            $user_info['user_id'] = $user_id;
            $user_info['user_name'] = $user_name;
            $user_info['token'] = $token;
            $user_info['s_name'] = $the_user['s_name'];
            $user_info['store_logo'] = $store['store_logo'];
            if ($openid) {
                //更新第三方登陆表
                $third_login = D("ThirdLogin")->get_info($openid);
                if (!$third_login) {
                    M("ThirdLogin")->add(array(
                        'third_name' => 'app',
                        'openid' => $openid,
                        'user_id' => $user_id,
                        'add_time' => time(),
                        'update_time' => time(),
                    ));
                } else {
                    M("ThirdLogin")->save(array(
                        'user_id' => $user_id,
                        'update_time' => time(),
                    ));
                }
            }
            render('10000', '登录成功', array(
                'user_info' => $user_info
            ));
        } else {
            if (!$user_name) {
                render('10111', '用户名为空');
            }
            if (!$password) {
                render('10112', '密码为空');
            }
        }
    }

    /**
     * @api {get} /index.php?m=Home&c=Index&a=logout 退出相关接口
     * @apiName 退出相关接口
     * @apiGroup All
     * @apiVersion 0.1.0 
     * @apiDescription 退出相关接口
     * 
     * @apiParam {String} user_name 用户名
     * @apiParam {String} password 密码
     * @apiParam {String} password 设备信息
     * 
     * @apiSuccess {String} user_id 日志ID
     */
    function logout() {
        if (!I('request.token')) {
            render('10001', '缺少参数');
        }
        M('AppToken')->where('token="' . I('request.token') . '"')->delete();
        render('10000', '退出成功');
    }

    /**
     * @api {get} /index.php?m=Home&c=Index&a=register 注册相关接口
     */
    function register() {
        $user_info = array();
        $user_name = I('request.user_name', '');
        $phone_mob = I('request.phone_mob', '');
        $openid = I('request.openid', '');
        $msg_sign = I('request.msg_sign', '');
        $msg_code = I('request.msg_code', '');
        $password = I('request.password', '');
        $city = I('request.city', '');
        $password_confirm = I('request.password_confirm', '');
        if (!$phone_mob || !$msg_sign || !$msg_code || !$password_confirm || !$password || !$city) {
            render('10001', '缺少参数',(object)array());
        }
        if (md5('HomeIndexSet_password' . $msg_code) != $msg_sign) {
            render('10016', '短信验证码错误',(object)array());
        }
        if (!$user_name) {
            $user_name = $phone_mob;
        }
        $user_name_len = strlen($user_name);
        if ($user_name_len < 3 || $user_name_len > 25) {
            render('10103', '注册失败，用户名长度为3-25之间！',(object)array());
        }
        if ($password != $password_confirm) {
            /* 两次输入的密码不一致 */
            render('10102', '输入两次密码不一致！',(object)array());
        }
        $passlen = strlen($password);
        if ($passlen < 6 || $passlen > 20) {
            render('10104', '密码长度必须大于6位！',(object)array());
        }
        //城市
        $res_city = M('region')->where(array('region_name'=>$city))->find();
        if(!$res_city){
            $cond['is_main'] = 1;
        }else{
            $cond['is_main'] = 0;
            $cond['city_id'] = $res_city['region_id'];
        }
        $res_store = M('member')->where($cond)->find();
        if(!$res_store){
            render('10104', '查找失败！',(object)array());
        }
        $user_id = D("Member")->do_register($user_name, $password, '', array('phone_mob' => $phone_mob, 's_id'=>$res_store['s_id'],'s_name'=>$res_store['s_name']));
        if ($user_id) {
            //登录赠送积分
            D('Integrallog')->change_integral_reg($user_id);
            $token = session_id();
            //保存到app_token表
            M('AppToken')->add(array(
                'user_id' => $user_id,
                'token' => $token
            ));
            $user_info['user_id'] = $user_id;
            $user_info['user_name'] = $phone_mob;
            $user_info['s_name'] = $res_store['s_name'];
            $user_info['store_logo'] = $res_store['store_logo'];
            $user_info['token'] = $token;
            if ($openid) {
                //记录登录设备信息
                $third_login = D("ThirdLogin")->get_info($openid);
                if (!$third_login) {
                    M("ThirdLogin")->add(array(
                        'third_name' => 'app',
                        'openid' => $openid,
                        'user_id' => $user_id,
                        'add_time' => time(),
                        'update_time' => time(),
                    ));
                } else {
                    M("ThirdLogin")->save(array(
                        'user_id' => $user_id,
                        'update_time' => time(),
                    ));
                }
            }
            render('10000', '注册成功', array(
                'user_info' => $user_info
            ));
        }
    }

    /**
     * @api {post} /index.php?m=Home&c=Index&a=set_password 设置登录密码
     * 
     */
    function set_password() {
        $password = I('request.password', '');
        $password_confirm = I('request.password_confirm', '');
        $user_id = I('request.user_id', '');
        $msg_sign = I('request.msg_sign', '');
        $msg_code = I('request.msg_code', '');
        if (!$user_id || !$password_confirm || !$password) {
            render('10001', '缺少参数');
        }
        if ($msg_sign) {
            if (md5('HomeIndexSet_password' . $msg_code) != $msg_sign) {
                render('10016', '短信验证码错误');
            }
        }
        $member = M('Member')->where('user_id=' . $user_id)->find();
        $available = array();
        if ($member['password']) {
            if (!$msg_sign) {
                render('10116', '用户已设置登录密码');
            }
        } else {
            $available = array('available' => 0);
        }
        if (!$member) {
            render('10106', '用户不存在');
        }


        if ($password != $password_confirm) {
            /* 两次输入的密码不一致 */
            render('10102', '输入两次密码不一致！');
        }
        $passlen = strlen($password);
        if ($passlen < 6 || $passlen > 20) {
            render('10104', '密码长度必须大于6位！');
        }

        M('Member')->where('user_id=' . $user_id)->save(array_merge(array('password' => md5($password)), $available));
        render('10000', '设置密码成功');
    }

    /**
     * @api {get} /index.php?m=Home&c=Index&a=find_password 找回密码接口
     * 
     */
    function find_password() {
        $user_name = I('request.user_name');
        if (!$user_name) {
            render('10111', '用户名为空');
        }
        $member = M('Member')->where('user_name="' . $user_name . '" OR email="' . $user_name . '" OR phone_mob="' . $user_name . '"')->find();
        if (!$member) {
            render('10106', '用户不存在');
        }
        if (!$member['phone_mob']) {
            render('10117', '用户未设置手机号');
        }
        $to_mobile = $member['phone_mob'];
        D('Msglog')->send_msg_system('find', $to_mobile);
        $msg_sign = md5('HomeIndexSet_password' . $_SESSION['MobileConfirmCode']);
        render('10000', '短信发送成功!', array('user_id' => $member['user_id'], 'msg_sign' => $msg_sign));
    }

    /**
     * @api {get} /index.php?m=Home&c=Index&a=send_messages 发送短信接口
     * 
     */
    function send_messages() {
        $to_mobile = I('request.mobile');
        if (!$to_mobile) {
            render('10012', '手机号码为空');
        }
        D('Msglog')->send_msg_system('register', $to_mobile);
        $msg_sign = md5('HomeIndexSet_password' . $_SESSION['MobileConfirmCode']);
        render('10000', '短信发送成功!', array('msg_sign' => $msg_sign));
    }

//    lichen 2019/6/5 start
    /**
     * @api {get} /index.php?m=Home&c=Index&a=revise_password 修改密码
     *
     */
    function revise_password() {
        $user_name = I('request.user_name');
        if (!$user_name) {
            render('10111', '用户名为空');
        }
        $member = M('Member')->where('user_name="' . $user_name . '" OR email="' . $user_name . '" OR phone_mob="' . $user_name . '"')->find();
        if (!$member) {
            render('10106', '用户不存在');
        }
        if (!$member['phone_mob']) {
            render('10117', '用户未设置手机号');
        }
        $to_mobile = $member['phone_mob'];
        D('Msglog')->send_msg_system('change', $to_mobile);
        $msg_sign = md5('HomeIndexSet_password' . $_SESSION['MobileConfirmCode']);
        render('10000', '短信发送成功!', array('user_id' => $member['user_id'], 'msg_sign' => $msg_sign));
    }
//    lichen 2019/6/5 end
    /**
     * @api {get} /index.php?m=Home&c=Index&a=scategory 获取所有店铺分类
     * 
     */
    function scategory() {
        $scategories = D("Scategory")->get_list();
        import("Org.Com.Tree");
        $tree = new \Org\Com\Tree;
        $tree->setTree($scategories, 'cate_id', 'parent_id', 'cate_name');
        $data = $tree->getArrayList(0);
        foreach ($data as $key1 => $val1) {
            if (!empty($val1['children'])) {
                foreach ($val1['children'] as $key2 => $val2) {
                    if (empty($val2['children'])) {
                        $data[$key1]['children'][$key2]['no_children'] = true;
                    }
                }
            } else {
                $data[$key1]['no_children'] = true;
            }
        }
        render('10000', 'OK', array('category' => $data));
    }

    /**
     * @api {get} /index.php?m=Home&c=Index&a=gcategory 获取所有商品分类
     * 
     */
    function gcategory() {
        $gcategories = D("Gcategory")->get_list();
        import("Org.Com.Tree");
        $tree = new \Org\Com\Tree;
        $tree->setTree($gcategories, 'cate_id', 'parent_id', 'cate_name');
        $data = $tree->getArrayList(0);
//        lichen 2019/5/30 start
        $res = M('banner_setting')->where(array('type_id'=>3))->select();
        $datar['banner'] = $res;
        $resr = M('person_setting')->order('sort_order')->select();
        $datar['person'] = $resr;
        $datar['catef'] = array();
        $datar['catel'] = array();
//        lichen 2019/5/30 end
        foreach ($data as $key1 => $val1) {
//                lichen 2019/5/30 start
            $data[$key1]['cate_logo'] = $gcategories[$val1['id']]['cate_logo'];
            $datar['catef'][] = $data[$key1];
//                lichen 2019/5/30 end
            if (!empty($val1['children'])) {
                foreach ($val1['children'] as $key2 => $val2) {
                    $data[$key1]['children'][$key2]['cate_logo'] = $gcategories[$val2['id']]['cate_logo'];
                    if (!empty($val2['children'])) {
//                lichen 2019/5/30 start
//                lichen 2019/5/30 end
                        foreach ($val2['children'] as $key3 => $val3) {
                            $data[$key1]['children'][$key2]['children'][$key3]['cate_logo'] = $gcategories[$val3['id']]['cate_logo'];
                            $datar['catel'][] = $data[$key1]['children'][$key2]['children'][$key3];
                        }
                    } else {
                        $data[$key1]['children'][$key2]['no_children'] = true;
                        $datar['catel'][] = $data[$key1]['children'][$key2];
                    }
                }
            } else {
                $data[$key1]['no_children'] = true;
                $datar['catel'][] = $data[$key1];
            }
        }
//        lichen 2019/5/30 start
        render('10000', 'OK', $datar);
//        render('10000', 'OK', array('category' => $data));
//        lichen 2019/5/30 end
    }

    /**
     * @api {get} /index.php?m=Home&c=Index&a=region_list 获得地区列表
     * 
     */
    function region_list() {
        $region_list = M('region')->select();
        import("Org.Com.Tree");
        $tree = new \Org\Com\Tree;
        $tree->setTree($region_list, 'region_id', 'parent_id', 'region_name');
        $data = $tree->getArrayList(0);
        foreach ($data as $key1 => $val1) {
            if (!empty($val1['children'])) {
                foreach ($val1['children'] as $key2 => $val2) {
                    if (empty($val2['children'])) {
                        $data[$key1]['children'][$key2]['no_children'] = true;
                    }
                }
            } else {
                $data[$key1]['no_children'] = true;
            }
        }
        render('10000', 'OK', array('region_list' => $data));
    }

    /**
     * @api {get} /index.php?m=Home&c=Index&a=recommend_goods 获得推荐产品列表
     * @apiName 获得推荐产品列表
     * @apiGroup All
     * @apiVersion 0.1.0 
     * @apiDescription 获得推荐产品列表
     * 
     * @apiSuccess {String} goods_id 商品ID
     * @apiSuccess {String} goods_name 商品名称
     * @apiSuccess {String} default_image 默认图片
     * @apiSuccess {String} price 价格
     * @apiSuccess {String} market_price 市场价格
     * 
     */
    function recommend_goods() {
        $map['if_show'] = 1;
        $map['mall_recommended'] = 1;
        $goods_list = M('goods')->field('goods_id,goods_name,default_image,price,market_price')
                ->where($map)
                ->order('mall_sort_order desc')
                ->select();
        render('10000', 'OK', array('goods_list' => $goods_list));
    }

    /**
     * @api {get} /index.php?m=Home&c=Index&a=recommend_store 获得推荐店铺列表
     * @apiName 获得推荐店铺列表
     * @apiGroup All
     * @apiVersion 0.1.0 
     * @apiDescription 获得推荐店铺列表
     * 
     * @apiSuccess {String} store_id 店铺ID
     * @apiSuccess {String} store_name 店铺名称
     * @apiSuccess {String} store_logo 店铺LOGO
     * 
     */
    function recommend_store() {
        $map['state'] = 1;
        $map['recommended'] = 1;
        $store_list = M('goods')->field('store_id,store_name,store_logo')
                ->where($map)
                ->order('sort_order desc')
                ->select();
        render('10000', 'OK', array('store_list' => $store_list));
    }

    /**
     * @api {get} /index.php?m=Home&c=Index&a=promotion_list 获得促销产品列表
     * @apiName 获得促销产品列表
     * @apiGroup All
     * @apiVersion 0.1.0 
     * @apiDescription 获得促销产品列表
     * 
     * @apiSuccess {String} pro_id 促销ID
     * @apiSuccess {String} goods_id 商品ID
     * @apiSuccess {String} pro_name 促销名称
     * @apiSuccess {String} start_time 开始时间
     * @apiSuccess {String} end_time 结束时间
     * @apiSuccess {String} pro_recommended 促销推荐
     * @apiSuccess {String} default_image 促销图片
     * @apiSuccess {String} price 正常价格
     * @apiSuccess {String} pro_price 促销价格
     * @apiSuccess {String} goods_name 商品名称
     * 
     */
    function promotion_list() {
        $time = time();
        $map['start_time'] = array('lt', $time);
        $map['end_time'] = array('gt', $time);
        $promotion_list = M('promotion')->field('pro.*,g.default_image,g.price,g.default_spec,g.goods_name,g.default_spec')
                ->alias('pro')
                ->join('LEFT JOIN ' . C('DB_PREFIX') . 'goods g ON g.goods_id = pro.goods_id')
                ->where($map)
                ->page(I('request.page', 1, 'intval') . ',10')
                ->select();
        if ($promotion_list) {
            foreach ($promotion_list as $key => $goods) {
                $pro_price = D('Promotion')->get_promotion_price($goods['goods_id'], $goods['default_spec']);
                if ($pro_price < $goods['price']) {
                    $promotion_list[$key]['pro_price'] = $pro_price;
                } else {
                    $promotion_list[$key]['pro_price'] = $goods['price'];
                }
            }
        }
        render('10000', '', array('promotion_list' => $promotion_list));
    }

    /**
     * @api {get} /index.php?m=Home&c=Index&a=search_goods 搜索相关产品
     */
     function search_goods() {
        $conditions = 'g.if_show=1 AND g.closed=0 AND s.state=1';
        $keyword = I('request.keyword', '');
        if ($keyword) {
            $conditions .= ' AND goods_name LIKE "%'.$keyword.'%"' ;
        }
        
        $cate_id = I('request.cate_id', '0', 'intval');
        if ($cate_id) {
            $conditions .= ' AND (cate_id_1=' . $cate_id . ' OR cate_id_2=' . $cate_id . ' OR cate_id_3=' . $cate_id . ' OR cate_id_4=' . $cate_id.')';
        }
        
        //排序
        $sort = I('request.sort', '');
        $order = I('request.order', '');
        if (!in_array($sort, array('sales', 'price', 'add_time', 'comments'))) {
            $sort = 'sales';
        }
        if (!in_array($order, array('desc', 'asc'))) {
            $order = 'desc';
        }
        $goods_mod = M('goods');
        $goods_list = $goods_mod
                ->where($conditions)
                ->field('g.goods_id,g.store_id,g.goods_name,g.default_image,g.price,gs.sales,gs.comments,g.add_time')->alias('g')
                ->join('LEFT JOIN ' . C('DB_PREFIX') . 'store s ON s.store_id = g.store_id')
                ->join('LEFT JOIN ' . C('DB_PREFIX') . 'goods_statistics gs ON gs.goods_id = g.goods_id')
                ->page(I('request.page', 1, 'intval') . ',10')
                ->order($sort . ' ' . $order)
                ->select();
        /*
        if ($goods_list) {
            foreach ($goods_list as $key => $goods) {
                $goods_list[$key]['price'] = D('Promotion')->get_promotion_price($goods['goods_id'], $goods['spec_id']);
            }
        }
         */
        $data = array(
            'goods_list' => $goods_list,
        );
        render('10000', 'OK', $data);
    }

    /**
     * @api {get} /index.php?m=Home&c=Index&a=search_store 搜索相关店铺
     * 
     */
    function search_store() {
        $map = array('state' => 1);
        $keyword = I('request.keyword', '');
        if ($keyword) {
            $map['store_name'] = array('LIKE', '%' . $keyword . '%');
        }

        $cate_id = I('request.cate_id', '0', 'intval');
        if ($cate_id) {
            $cate_ids = D('Scategory')->get_descendant($cate_id);
            $map['cate_id'] = array('IN', implode(',', $cate_ids));
        }

        //排序
        $sort = I('request.sort', '');
        $order = I('request.order', '');
        if (!in_array($sort, array('sales', 'credit_value', 'add_time', 'praise_rate'))) {
            $sort = 'sales';
        }
        if (!in_array($order, array('desc', 'asc'))) {
            $order = 'desc';
        }


        $store_mod = M('store');
        $subQuery = M('Store')->alias('store')->field('COUNT(*) as sales,store.store_id')->join('LEFT JOIN ' . C('DB_PREFIX') . 'order order_alias ON store.store_id = order_alias.seller_id')->group('store_id')->buildSql();
        $store_list = $store_mod->field('store.store_id,store.store_name,store.store_logo')
                ->alias('store')
                ->where($map)
                ->join('LEFT JOIN ' . $subQuery . ' a ON a.store_id=store.store_id')
                ->join('LEFT JOIN ' . C('DB_PREFIX') . 'category_store category_store ON store.store_id = category_store.store_id')
                ->order($sort . ' ' . $order)
                ->page(I('request.page', 1, 'intval') . ',10')
                ->select();
        foreach ($store_list as $key => $val) {
            $store_list[$key]['goods_list'] = M('Goods')->field('goods_id,goods_name,default_image,price')->where('store_id=' . $val['store_id'] . ' AND if_show=1 AND closed=0')->order('add_time desc')->limit(12)->select();
            $store_list[$key]['total_count'] = M('Goods')->where('store_id=' . $val['store_id'] . ' AND if_show=1 AND closed=0')->count();
        }
        $data = array(
            'store_list' => $store_list,
        );
        render('10000', 'OK', $data);
    }

    function homepage(){
        if(!$_POST['page'] || !$_POST['pagenum']){
            render('10000', '参数错误');
        }
        if($_POST['page'] == 1){
            $resBanner = M('banner_setting')->where(array('type_id' => 1))->order('sort_order')->limit(5)->select();
            $data['banner'] = $resBanner;
            $resSort = M('sort_setting')->where()->order('sort_order')->limit(3)->select();
            $data['sort'] = $resSort;
        }

        $resTheme = M('theme_setting')->where()->order('sort_order')->limit($_POST['pagenum'])->page($_POST['page'])->select();
        $data['theme'] = $resTheme;
//        if($_POST['page'] % 2 == 1){
        $resRecommend = M('recommend_setting')->where()->order('sort_order')->limit(1)->page($_POST['page']/2+1)->find();
        if($resRecommend['goods_id']){
            $cond['goods_id'] = array('in',$resRecommend['goods_id']);
            $res = M('goods')->where($cond)->select();
            $resRecommend['goods'] = $res;
            $condCount['is_show'] = 1;
            $condCount['closed'] = 0;
            $condCount['add_time'] = array('between',array(time()-7*86400,time()));
            $resRecommend['recommend_count'] = M('goods')->where($condCount)->count();
        }else{
            $resRecommend = json_decode('{}');
        }
        $resLimited = M('limited_setting')->find();
        $data['limited'] = $resLimited;
        $data['recommend'] = $resRecommend;

//        }else{
//            $data['recommend'] = json_decode('{}');
//        }
        $data['hot_search'] = explode(',',C('HOT_SEARCH'));
        $data['search_keywords'] = C('SEARCH_KEYWORDS');
        render('10000', 'OK', $data);
    }

    /**
     * 递归查出所有分类
     */
    function getTree($data,$cate_id){
        $tree = array();
        foreach($data as $k => $v) {

            if($v['parent_id'] == $cate_id)
            {
                $v['newArr'] = $this->getTree($data, $v['cate_id']);
                if(is_array($v['newArr']) && !empty($v['newArr'])){
                    foreach($v['newArr'] as $key=>$value){
                        $tree[]['cate_id'] = $value['cate_id'];
                    }
                }
                $tree[]['cate_id'] = $v['cate_id'];
            }
        }
        return $tree;
    }

    /**
     * 查询分类的方法
     * lixinguo
     */
    function get_cateList($cate_id)
    {

        //todo 获取当前分类ID是否为父级分类
        $cateModel = D("gcategory");
        $cateInfo = $cateModel->select();
        $treeList = $this->getTree($cateInfo,$cate_id);
        //todo 当前分类是父级分类
        //todo 获取当前父级分类下的所有子分类
        $arr = array();
        if(!empty($treeList)){
            $ids = '';
            foreach($treeList as $k=>$v){
                $ids .= $v['cate_id'] .',';
            }
            //todo 当前分类ID是父级
            $arr['parent_id'] = 9999;
            $arr['ids'] = $ids .$cate_id;
        }else{
            //todo 当前分类ID不是父级
            $arr['parent_id'] = 1111;
            $arr['ids'] = $cate_id;
        }
        return $arr;

    }

//    lichen 2019/5/30 start

    function get_list(){
        $conditions = 'g.if_show=1 AND g.closed=0 AND s.state=1';
        $keyword = I('request.keyword', '');
        if ($keyword) {
            $conditions .= ' AND goods_name LIKE "%'.$keyword.'%"';
        }

        $brand = I('request.brand', '');
        if ($brand) {
            $conditions .= ' AND brand LIKE "%'.$brand.'%"';
        }

        $cate_id = I('request.cate_id', '0', 'intval');
        if ($cate_id) {
//            $conditions .= ' AND (cate_id_1=' . $cate_id . ' OR cate_id_2=' . $cate_id . ' OR cate_id_3=' . $cate_id . ' OR cate_id_4=' . $cate_id.')';

            //todo 调取分类查询
            $result = $this->get_cateList($cate_id);
            if($result['parent_id'] == 9999){
                $conditions .= ' AND cate_id IN ('.$result['ids'].')';
            }else{
                $conditions .= ' AND cate_id='.$result['ids'];
            }
        }


        //排序
        $sort = I('request.sort', '');
        $order = I('request.order', '');
        if (!in_array($sort, array('sales', 'price', 'add_time', 'comments'))) {
            $sort = 'sales';
        }
        if (!in_array($order, array('desc', 'asc'))) {
            $order = 'desc';
        }
        //goods_id
        $goods_id = I('request.goods_id', '');
        if(trim($goods_id)){
            $conditions .= ' AND g.goods_id IN ('.$goods_id.')';
        }

        $goods_mod = M('goods');

        $goods_list = $goods_mod
            ->where($conditions)
            ->field('g.brand,g.goods_id,g.store_id,g.goods_name,g.default_image,g.price,g.market_price,gs.sales,gs.comments,g.add_time')->alias('g')
            ->join('LEFT JOIN ' . C('DB_PREFIX') . 'store s ON s.store_id = g.store_id')
            ->join('LEFT JOIN ' . C('DB_PREFIX') . 'goods_statistics gs ON gs.goods_id = g.goods_id')
            ->page(I('request.page', 1, 'intval') . ',10')
            ->order($sort . ' ' . $order)
            ->select();
//        $_POST['user_id'] = 3;
        if($_POST['user_id']){
            $cond_collect['user_id'] = $_POST['user_id'];
            $res_collect = M('collect')->where($cond_collect)->field('item_id as goods_id ,user_id')->select();
            foreach ($goods_list as $key => $value){
                $res_spec = M('goods_spec')->group('spec_1value')->where(array('goods_id'=>$value['goods_id']))->field('spec_1')->select();
                $data = array_column($res_spec,'spec_1');
                $goods_list[$key]['spec'] = $data;
                if($res_collect){
                    if(in_array($goods_list[$key]['goods_id'],array_column($res_collect,'goods_id'))){
                        $goods_list[$key]['collect'] = true;
                    }else{
                        $goods_list[$key]['collect'] = false;
                    }
//                    if($goods_list[$key]['goods_id'] == 21){
//                        var_dump($goods_list[$key]['collect']);exit;
//                    }
                }else{
                    $goods_list[$key]['collect'] = false;
                }
            }
        }else{
            foreach ($goods_list as $key => $value){
                $res_spec = M('goods_spec')->group('spec_1value')->where(array('goods_id'=>$value['goods_id']))->field('spec_1')->select();
                $data = array_column($res_spec,'spec_1');
                $goods_list[$key]['spec'] = $data;
            }
        }
        /*
        if ($goods_list) {
            foreach ($goods_list as $key => $goods) {
                $goods_list[$key]['price'] = D('Promotion')->get_promotion_price($goods['goods_id'], $goods['spec_id']);
            }
        }
         */
        if(!$goods_list){
            $goods_list = array();
        }
        $res = M('banner_setting')->where(array('type_id'=>2))->select();
        $data = array(
            'goods_list' => $goods_list,
            'banner' => $res
        );
        render('10000', 'OK', $data);
    }

    function get_theme(){
        if(!$_POST['theme_id']){
            render('10004', '参数错误');
        }

        $res = M('theme_setting')->where(array('theme_id'=>$_POST['theme_id']))->find();
        if(!$res){
            render('10004', '参数错误');
        }
        $cond['goods_id'] = array('IN',$res['goods_id']);
        $res_goods = M('goods')->limit($_POST['pagenum'])->page($_POST['page'])->where($cond)->select();
        if($_POST['user_id']){
            $cond_collect['user_id'] = $_POST['user_id'];
            $cond_collect['item_id'] = array('IN',$res['goods_id']);
            $res_collect = M('collect')->where($cond_collect)->field('item_id as goods_id ,user_id')->select();
            foreach ($res_goods as $key => $value){
                $res_spec = M('goods_spec')->group('spec_1value')->where(array('goods_id'=>$value['goods_id']))->field('spec_1')->select();
                $data = array_column($res_spec,'spec_1');
                $res_goods[$key]['spec'] = $data;
                if($res_collect){
                    foreach ($res_collect as $k => $v) {
                        if(!$res_goods[$key]['collect']){
                            if($value['goods_id'] == $v['goods_id']){
                                $res_goods[$key]['collect'] = true;
                            }else{
                                $res_goods[$key]['collect'] = false;
                            }
                        }
                    }
                }else{
                    $res_goods[$key]['collect'] = false;
                }
            }
        }else{
            foreach ($res_goods as $key => $value){
                $res_spec = M('goods_spec')->group('spec_1value')->where(array('goods_id'=>$value['goods_id']))->field('spec_1')->select();
                $data = array_column($res_spec,'spec_1');
                $res_goods[$key]['spec'] = $data;
            }
        }
        $data = array(
            'theme' => $res,
            'goods' => $res_goods
        );
        render('10000', 'OK', $data);
    }

    function limitedtime_list(){
        if(!$_POST['page'] || !$_POST['pagenum']){
            render('10000', '参数错误');
        }
        $cond['end_time'] = array('gt',date("Y-m-d H:i:s"));
        $res = M('limitedtime_setting')->where($cond)->limit($_POST['pagenum'])->page($_POST['page'])->order('sort_order desc')->select();
        if($res){
            foreach($res as $key => $value){
                $res[$key]['start_time'] = strtotime($value['start_time']);
                $res[$key]['end_time'] = strtotime($value['end_time']);
            }
        }else{
            render('10000', 'OK', json_decode('{}'));
        }

        render('10000', 'OK', array('data'=>$res));
    }

//    lichen 2019/5/30 end

    /**
     * @api {get} /index.php?m=Home&c=Index&a=get_userBanner 显示用户banner
     *
     */
    public function get_userBanner(){
        $resBanner = M('banner_setting')->where(array('type_id' => 4))->order('sort_order')->limit(5)->select();
        $data['banner'] = $resBanner;
        render('10000', '', $data);
    }

}
